package com.absorbx.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.absorbx.jcr.dao.ContentDao;
import com.absorbx.jcr.domain.Content;
import com.absorbx.security.Role;
import com.absorbx.service.SystemService;

public class SystemServiceImpl implements SystemService {
	final Logger logger = LoggerFactory.getLogger(SystemServiceImpl.class);
	private String appPath;
	private String systemPath;
	private String userPath;
	private String rolePath;
	private String defaultRole;
	public ContentDao contentDao;

	public void initContents() {
		logger.info("initilize system path:"+systemPath);
		this.createContent(systemPath);
		logger.info("initilize user path:"+userPath);
		this.createContent(userPath);
		logger.info("initilize role path:"+rolePath);
		this.createContent(rolePath);
		

	}
	
	public void setContentDao(ContentDao contentDao) {
		this.contentDao = contentDao;
	}
	
	public void createContent(String path) {
		String[] array = path.split("/");
		String p = "";
		for (int i = 0; i < array.length; i++) {
			String cut = array[i];
			if (cut != null && cut.length() > 0) {
				p = p + "/" + cut;
				this.createContentByPath(p);
			}
		}
	}

	private void createContentByPath(String path) {
		if (contentDao.findByPath(path) == null) {
			Content content = new Content();
			content.setPath(path);
			logger.info("create direcotry:" + content.getPath());
			int lastIndex = path.lastIndexOf("/");
			content.setNodeName(path.substring(lastIndex + 1, path.length()));
			content.setTitle(path);
			contentDao.save(content);
		}

	}
	public void setAppPath(String appPath) {
		this.appPath = appPath;
	}
	public void setSystemPath(String systemPath) {
		this.systemPath = systemPath;
	}
	public void setUserPath(String userPath) {
		this.userPath = userPath;
	}
	public void setRolePath(String rolePath) {
		this.rolePath = rolePath;
	}
	public void setDefaultRole(String defaultRole) {
		this.defaultRole = defaultRole;
	}
	
	
}
